import os
import re
import json
import argparse
import glob
import numpy as np
from copy import deepcopy
from pprint import pprint


def get_scores_from_list(score_list):
    score_dict = {}
    for item in score_list:
        for key, value in item.items():
            query_id, ans_id = key.split(':')
            if query_id in score_dict:
                if ans_id in score_dict[query_id] and value != score_dict[query_id][ans_id]:
                    print(f">>>>> warning!")
                    print(f">>>>> replacing {query_id}: {ans_id} value {score_dict[query_id][ans_id]} with {value}")

                score_dict[query_id][ans_id] = value
            else:
                score_dict[query_id] = {ans_id: value}
    return score_dict


def evaluation(score_path=None):
    if score_path is not None:
        with open(score_path, 'r') as f:
            score_list = json.load(f)
        data_scores = get_scores_from_list(score_list)
    scores = []
    for key, item in data_scores.items():
        for ans_key, ans_score in item.items():
            scores.append(ans_score)
    mean = np.mean(scores)
    var = np.var(scores)
    max = np.max(scores)
    min = np.min(scores)
    return {
        'mean':mean,
        'var':var,
        'max':max,
        'min':min,
        'data':scores
    }


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description ='parser for preference data processing.')
    parser.add_argument("--output_file_name", type=str, default="", help="the path to output data.")
    parser.add_argument("--score_path", type=str, default="", help="the rm model name to get score")
    args = parser.parse_args()

    outputs = evaluation(args.score_path)
    
    with open(f"{args.output_file_name}", 'w', encoding="utf-8") as f:
        json.dump(outputs, f, ensure_ascii=False, indent=2)

    
